package com.ticker.national;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import android.net.http.AndroidHttpClient;

import com.ticker.national.util.Log;

public class FTAParseTask extends BaseAsyncTask<Void, Void, Void> {
	
	private final String FTA_INFO_URL ="http://national-assembly.googlecode.com/files/2011fta.txt";
	private final String TAG = "FTAParseTask";
	private ArrayList<FTAInfo> mFTAInfoList;

	public FTAParseTask(OnTaskListener l) {
		super(l);
	}
	
	public FTAParseTask(final ArrayList<FTAInfo> ftaInfoList, OnTaskListener listener) {
		super(listener);
		this.mFTAInfoList = ftaInfoList;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		mFTAInfoList.clear();
	}
	
	@Override
	protected Void doInBackground(Void... arg0) {
		downloadFile();
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
	}
	
	private InputStream downloadFile() {
		final HttpClient client = AndroidHttpClient.newInstance(this.getClass().getName());
		final HttpGet getRequest = new HttpGet(FTA_INFO_URL);
		HttpResponse response;
		InputStream inputStream = null;

		try {
			response = client.execute(getRequest);
			int statusCode = response.getStatusLine().getStatusCode();

			if(statusCode != HttpStatus.SC_OK) {
				Log.e(TAG, "statusCode : " + statusCode);
				return null;
			}

			final HttpEntity entity = response.getEntity();
			
			inputStream = entity.getContent();
			setFTAInfo(inputStream);
			entity.consumeContent();
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(client instanceof AndroidHttpClient) {
				((AndroidHttpClient)client).close();
			}
		}
		
		return inputStream;
	}
	
	private void setFTAInfo(InputStream is) {
		BufferedReader in;
		
		if(is == null)
			return;
		
		try {
			in = new BufferedReader(new InputStreamReader(is, "MS949"));
			String s;
			int i = 0;
			while ((s = in.readLine()) != null) {
				String[] split = s.split(",");
				FTAInfo ftaInfo = new FTAInfo(split[0].trim(), split[1].trim());
				mFTAInfoList.add(ftaInfo);
				i++;
			}
			Log.d(TAG, "i: " + i);
			in.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public class FTAInfo {
		
		String name;
		String party;
		
		public FTAInfo(String name, String party) {
			this.name = name;
			this.party = party;
		}
		
		public String getName() {
			return name;
		}

		public String getParty() {
			return party;
		}

		@Override
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("Name : " + name);
			sb.append(", Party : " + party);
			
			return sb.toString();
		}
	}
}
